/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.rs.domain.resp.GetTechniqueResponse;
import com.xy.biz.rs.domain.query.TechniqueQuery;
import com.xy.biz.rs.domain.converter.TechniqueConverter;
import com.xy.biz.rs.client.TechniqueClient;
import com.xy.biz.rs.service.TechniqueService;

/**
 *
 *
 * @author wwj
 * @date 2021-4-13
 */
@Api(tags = "生产工艺")
@Slf4j
@RestController
@RequestMapping("/api/rs/technique")
public class TechniqueController implements TechniqueClient {

	@Autowired
	private TechniqueService techniqueService;

	@ApiOperation(value = "保存生产工艺")
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveTechnique(@RequestBody SaveTechniqueRequest request) {
		return techniqueService.saveTechnique(request);
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllTechnique(@RequestBody Collection<SaveTechniqueRequest> collection) {
		techniqueService.saveAllTechnique(TechniqueConverter.toBO(collection));
		return Result.success();
	}

	@ApiOperation(value = "更新生产工艺")
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateTechnique(@RequestBody UpdateTechniqueRequest request) {
		return techniqueService.updateTechnique(request);
	}

	@ApiOperation(value = "删除生产工艺")
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteTechnique(@RequestBody GetTechniqueRequest request) {
		return techniqueService.deleteTechnique(request.getTechniqueId());
	}

	@ApiOperation(value = "获取生产工艺")
	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetTechniqueResponse> getTechnique(@RequestBody GetTechniqueRequest request) {
		return Result.success(TechniqueConverter.toResponse(techniqueService.getTechnique(request.getTechniqueId())));
	}

	@ApiOperation(value = "根据产品项目获取生产工艺")
	@Validatable
	@PostMapping(value = "/get_by_dict_code")
	public Result<GetTechniqueResponse> getTechniqueByDictCode(@RequestBody GetByProductDictCodeRequest request) {
		return techniqueService.getByProductDictCode(TechniqueConverter.toBO(request));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetTechniqueResponse>> listTechniqueById(@RequestBody Collection<Long> collection) {
		return Result.success(TechniqueConverter.toResponse(techniqueService.listTechniqueById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetTechniqueResponse>> listTechnique(@RequestBody ListTechniqueRequest request) {
		return Result.success(TechniqueConverter.toResponse(techniqueService.listTechnique(TechniqueConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countTechnique(@RequestBody ListTechniqueRequest request) {
		return Result.success(techniqueService.countTechnique(TechniqueConverter.toQuery(request)));
	}

	@ApiOperation(value = "生产工艺列表")
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetTechniqueResponse>> pageTechnique(@RequestBody ListTechniqueRequest request) {
		TechniqueQuery query = TechniqueConverter.toQuery(request);
		List<GetTechniqueResponse> list = TechniqueConverter.toResponse(techniqueService.listTechnique(query));
		int count = techniqueService.countTechnique(query);
		return Result.success(new Page<>(list, count));
	}

}
